package archer.framework.utils;

import jodd.typeconverter.Convert;
import jodd.util.PropertiesUtil;

import java.util.Properties;

/**
 * Properties工具类
 *
 * @author christ
 * @date 2015/8/27
 */
public class PropertiesUtils extends PropertiesUtil {

    /**
     * 获得int值，如果值为null则使用默认值0
     *
     * @param properties
     * @param key
     * @return
     */
    public static int getInt(Properties properties, String key) {
        return Convert.toIntValue(properties.getProperty(key));
    }

    /**
     * 获得int值
     *
     * @param properties
     * @param key
     * @param value
     * @return
     */
    public static int getInt(Properties properties, String key, int value) {

        String o = properties.getProperty(key);
        if (ValidateUtils.isEmpty(o)) {
            return value;
        }
        return Convert.toIntValue(properties.getProperty(key), value);
    }

    public static long getLong(Properties properties, String key) {
        return Convert.toLong(properties.getProperty(key));
    }

    public static long getLong(Properties properties, String key, long value) {

        String o = properties.getProperty(key);
        if (ValidateUtils.isEmpty(o)) {
            return value;
        }
        return Convert.toLong(properties.getProperty(key), value);
    }

    public static boolean getBoolean(Properties properties, String key) {

        return Convert.toBoolean(properties.getProperty(key));
    }

    public static boolean getBoolean(Properties properties, String key, boolean value) {

        String o = properties.getProperty(key);
        if (ValidateUtils.isEmpty(o)) {
            return value;
        }
        return Convert.toBoolean(properties.getProperty(key), value);
    }

    /**
     * 获得String值
     *
     * @param properties
     * @param key
     * @return
     */
    public static String getString(Properties properties, String key) {
        return properties.getProperty(key);
    }

    /**
     * 获得String值
     *
     * @param properties
     * @param key
     * @param value
     * @return
     */
    public static String getString(Properties properties, String key, String value) {
        String o = properties.getProperty(key);
        if (ValidateUtils.isEmpty(o)) {
            return value;
        }
        return properties.getProperty(key, value);
    }
}
